package com.baidu.kshpt.config;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baidu.kshpt.entity.UploadResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author dengze_邓泽
 * @Date 2021/5/31 17:40
 * @Version 1.0
 */
@Component
public class GoFastdfsClient {

    @Value("${camera.upload.path}")
    private String uploadPath;

    @Value("${camera.delete.path}")
    private String deletePath;

    public UploadResult upload(MultipartFile file, String sixCode) throws IOException {
        UploadResult uploadResult = new UploadResult();
        ByteArrayOutputStream bos = addWatermark(file, sixCode);
        byte[] b = bos.toByteArray();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b);
        InputStreamResource isr = new InputStreamResource(byteArrayInputStream, file.getOriginalFilename());
        Map<String, Object> params = new HashMap<>();
        params.put("file", isr);
        params.put("path", "image");
        params.put("output", "json");
        // 场景
        params.put("scene", "image");
        String resp = HttpUtil.post(uploadPath, params);
        JSONObject exJson = JSONObject.parseObject(resp);
        uploadResult = JSONObject.toJavaObject(exJson, UploadResult.class);
        return uploadResult;
    }

    /**
     * 图片删除
     *
     */
    public void deleteImage(String md5) {
        if (StringUtils.isEmpty(md5)) {
            return;
        }
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("md5", md5);
            HttpUtil.post(deletePath, params);
        } catch (Exception e) {
            new RuntimeException(e.getMessage());
        }
    }

    /**
     * 加水印
     *
     * @param myfile
     * @param sixCode
     * @return
     * @throws IOException
     */
    private ByteArrayOutputStream addWatermark(MultipartFile myfile, String sixCode) throws IOException {
        InputStream in = myfile.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(in);
        BufferedImage image = ImageIO.read(bis);
        int height = image.getHeight();
        int width = image.getWidth();
        // 加水印
        Graphics2D g = image.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.setColor(new Color(128, 128, 128));
        // 字体
        int num = 0;
        if (width > height) {
            num = height / 30;
        } else {
            num = width / 30;
        }
        g.setFont(new Font("微软雅黑", Font.PLAIN, num));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(new Date());
        String watermarkContent = "拍摄时间:" + date + "&摄像头编码:" + sixCode;
        // 设置水印坐标
        String[] split = watermarkContent.split("&");
        int x = 10;
        int y = height - 10;
        for (int i = 0; i < split.length; i++) {
            g.drawString(split[i], x, y -= g.getFontMetrics().getHeight());
        }
        g.dispose();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", bos);
        return bos;
    }

}
